package com.tamecode.chapter06.sec45;

import org.junit.Test;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @Author: Qiongchao
 * @Version: 1.0.0
 * @Date: 2023/9/26
 */
public class I18nTest {

    /**
     * 本地化初始化
     * 语言 IOS639-1 代码查询：https://www.loc.gov/standards/iso639-2/php/English_list.php
     * 地区 IOS-3166 查询：https://www.iso.org/iso-3166-country-codes.html （https://www.iso.org/obp/ui/#search/code/）
     */
    public void localeTest() {
        //①带有语言和国家/地区信息的本地化对象
        Locale locale1 = new Locale("zh","CN");
        //②只有语言信息的本地化对象
        Locale locale2 = new Locale("zh");
        //③等同于Locale("zh","CN")
        Locale locale3 = Locale.CHINA;
        //④等同于Locale("zh")
        Locale locale4 = Locale.CHINESE;
        //⑤获取本地系统默认的本地化对象
        Locale locale5 = Locale.getDefault();
    }

    @Test
    public void numberFormat() {
        Locale locale = new Locale("zh", "CN");
        NumberFormat currFmt = NumberFormat.getCurrencyInstance(locale);
        double amt = 123456.78;
        // ￥123,456.78
        System.out.println(currFmt.format(amt));
        // $123,456.78
        System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(amt));
    }

    @Test
    public void dateFormat() {
        Locale locale = new Locale("en", "US");
        Date date = new Date();
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        // Sep 26, 2023
        System.out.println(df.format(date));
        // 2023-9-26
        System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA).format(date));
    }

    @Test
    public void messageFormat() {
        //①格式化信息串
        String pattern1 = "{0}，你好！你于{1}在工商银行存入{2} 元。";
        String pattern2 = "At {1,time,short} On{1,date,long}，{0} paid {2,number, currency}.";
        //②用于动态替换占位符的参数
        Object[] params = {"John", new GregorianCalendar().getTime(),1.0E3}; // E3 表示10的3次方
        //③使用默认的本地化对象格式化信息
        String msg1 = MessageFormat.format(pattern1,params);
        //④使用指定的本地化对象格式化信息
        MessageFormat mf = new MessageFormat(pattern2,Locale.US);
        String msg2 = mf.format(params);
        System.out.println(msg1); // John，你好！你于23-9-26 下午10:54在工商银行存入1,000 元。
        System.out.println(msg2); // At 10:54 PM OnSeptember 26, 2023，John paid $1,000.00.
    }

    @Test
    public void resourceBundleTest() {
        // 注意：resource：为文件名的前缀
        ResourceBundle rb1 = ResourceBundle.getBundle("sec6-45/i18n/resource", Locale.US);
        ResourceBundle rb2 = ResourceBundle.getBundle("sec6-45/i18n/resource", Locale.CHINA);
        /*
        us:How are you!
        cn:您好！
         */
        System.out.println("us:"+rb1.getString("greeting.common"));
        System.out.println("cn:"+rb2.getString("greeting.common"));
    }

    @Test
    public void resourceBundleAndMessageFormatTest() {
        // 注意：fmt_resource：为文件名的前缀
        //①加载本地化资源
        ResourceBundle rb1 =
                ResourceBundle.getBundle("sec6-45/i18n/fmt_resource",Locale.US);
        ResourceBundle rb2 =
                ResourceBundle.getBundle("sec6-45/i18n/fmt_resource",Locale.CHINA);
        Object[] params = {"John", new GregorianCalendar().getTime()};

        // 用本地化对象进行格式化
        String str1 = new MessageFormat(rb1.getString("greeting.common"),Locale.US).format(params);
        String str2 =new MessageFormat(rb2.getString("greeting.morning"),Locale.CHINA).format(params);
        String str3 =new MessageFormat(rb2.getString("greeting.afternoon"),Locale.CHINA).format(params);
        System.out.println(str1); // How are you!John,today is 9/27/23 11:07 PM
        System.out.println(str2); // 早上好!John, 现在是 下午11:07
        System.out.println(str3); // 下午好!John, 现在是 2023年9月27日
    }


}
